const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const db = require('../model/index');
const User = db.models.User;
const WorkingSchedule = db.models.WorkingSchedule;
const moment = require('moment');

class WorkingScheduleController {
  async getTeamMember(ctx) {
    let teamMember = await User.findAll({
      attributes: ['username', 'teamName'],
      where: {
        isStop: false,
      }
    });
    ctx.body = {
      code: 0,
      msg: {
        teamMember
      }
    }
  }

  /**
   * 最好不要在这里自动生成，情况过多，复杂度过高
   */
  async getScheduleList(ctx) {
    let { year, month } = ctx.request.body;
    let resultArr = await WorkingSchedule.findAll({  // 一次性获取一个月的数据，并按照日期排序，根据创建顺序，白班会在夜班前面
      order: [
        ['day', 'ASC'],
        ['month', 'ASC'],
        ['year', 'ASC'],
      ],
      where: {
        year,
        month: month,
      }
    });
    let day;
    let scheduleList = [];
    for( let d = 1; d <= 31; d += 1) {
      scheduleList.push([]);
    }
    resultArr.forEach((ele) => {
      // if(day !== ele.day) {  // 如果是day发生了变化，则另起一天
      //   day = ele.day;
      //   scheduleList.push([]);  // 加入一个数组
      // }
      scheduleList[ele.day - 1].push(ele);
    });
    ctx.body = {
      code: 0,
      msg: {
        scheduleList
      }
    }
  }

  // startTime timestamps
  // endTime timestamps
  async addSchedule(ctx) {
    let { year, month, day, teamName, startTime, endTime, scheduleType } = ctx.request.body;
    let startHour = moment(startTime).hour();
    let startMinute = moment(startTime).minute();
    let endHour = moment(endTime).hour();
    let endMinute = moment(endTime).minute();

    await WorkingSchedule.create({
      year,
      month,
      day,
      teamName,
      startHour,
      startMinute,
      endHour,
      endMinute,
      // startTime: new Date(startTime),  // 排班具体开始时间
      // endTime: new Date(endTime),  // 排班具体结束时间
      startTime: {
        year,
        month,
        day,
        startHour,
        startMinute,
      },
      endTime: {
        year,
        month,
        day,
        startHour,
        startMinute,
        endHour,
        endMinute,
      },
      scheduleType,
    });
    let newTableData = await WorkingSchedule.findAll({
      where: {
        year,
        month,
        day,
      }
    });
    let arr = [];
    arr = await WorkingSchedule.findAll({
      where: {
        year,
        month: month,
      }
    });
    day = 1;
    let newList = [];
    for(; day <= 31; day += 1) {
      newList.push([]);
    }
    arr.forEach((r) => {
      newList[r.day - 1].push(r);
    });
    ctx.body = {
      code: 0,
      msg: {
        newTableData,
        newList,
      }
    }
  }

  async updateSchedule(ctx) {
    let { id, year, month, day, teamName, scheduleType, startTime, endTime, } = ctx.request.body;
    let startHour = moment(startTime).hour();
    let startMinute = moment(startTime).minute();
    let endHour = moment(endTime).hour();
    let endMinute = moment(endTime).minute();

    await WorkingSchedule.update({
      teamName,
      startHour,
      startMinute,
      endHour,
      endMinute,
      startTime: {
        year,
        month,
        day,
        startHour,
        startMinute,
      },
      endTime: {
        year,
        month,
        day,
        startHour,
        startMinute,
        endHour,
        endMinute,
      },
      scheduleType,
      year,
      month,
      day,
    }, {
      where: {
        id,
      }
    });
    let newTableData = await WorkingSchedule.findAll({
      where: {
        year,
        month,
        day,
      }
    });
    let newList = [];
    let arr = await WorkingSchedule.findAll({
      where: {
        month,
        year,
      }
    });
    day = 1;
    for(; day <= 31; day += 1) {
      newList.push([]);
    }
    arr.forEach((r) => {
      newList[r.day - 1].push(r);
    });

    ctx.body = {
      code: 0,
      msg: {
        newTableData,
        newList,
      }
    }
  }

  async deleteSchedule(ctx) {
    let { id } = ctx.request.body;
    let schedule = await WorkingSchedule.findOne({
      where:{
        id,
      }
    });
    let { year, month, day } = schedule;

    await WorkingSchedule.destroy({
      where:{
        id,
      }
    });

    let newTableData = await WorkingSchedule.findAll({
      where: {
        year,
        month,
        day,
      }
    });
    let newList = [];
    let arr = await WorkingSchedule.findAll({
      where: {
        month,
        year,
      }
    });
    day = 1;
    for(; day <= 31; day += 1) {
      newList.push([]);
    }
    arr.forEach((r) => {
      newList[r.day - 1].push(r);
    });

    ctx.body = {
      code: 0,
      msg: {
        newTableData,
        newList,
      }
    }
  }

  async getExportData(ctx) {
    let { year, month } = ctx.request.body;
    let scheduleData = await WorkingSchedule.findAll({
      where: {
        year,
        month,
      },
      order: [
        ['day', 'ASC']
      ]
    });
    let users = await User.findAll({
      where: {
        [Op.and]: [{
          teamName: {
            [Op.not]: '无'
          }
        },{
          teamName: {
            [Op.not]: null
          }
        }],
        isStop: false,
      },
      order: [
        ['teamName', 'ASC']
      ]
    });
    let exportTableData = [];
    users.forEach((user) => {
      let duty = [];

      scheduleData.forEach((schedule) => {
        if ( schedule.teamName === user.teamName ) {
          duty.push({
            type: schedule.scheduleType,
            day: schedule.day,
          });
        }
      });

      exportTableData.push({
        userName: user.username,
        teamName: user.teamName,
        duty,
      })
    });

    ctx.body = {
      code: 0,
      msg: {
        exportTableData,
      }
    }
  }
}

module.exports = new WorkingScheduleController();
